import 'dart:core';
import 'dart:developer';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:kasax_demos2/entry/entrys.dart';
import 'package:kasax_demos2/widgets/posts_widgets.dart';

import 'sliver_list_demo.dart';

/// sliverappbar
class SliverAppbarDemo extends StatefulWidget {
  const SliverAppbarDemo({Key? key}) : super(key: key);

  @override
  _SliverAppbarDemoState createState() => _SliverAppbarDemoState();
}

// SliverGridDelegateWithFixedCrossAxisCount设置交叉轴上的条目的数量
class _SliverAppbarDemoState extends State<SliverAppbarDemo> {
  bool pinned = false;
  bool floating = false;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SliverAppBarDemoWidget(pinned, floating),
      floatingActionButton: Container(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.end,
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            FloatingActionButton(
              onPressed: () {
                setState(() {
                  floating = false;
                  pinned = true;
                });
              },
              child: Text("固定"),
            ),
            SizedBox(
              height: 15,
            ),
            FloatingActionButton(
              onPressed: () {
                setState(() {
                  floating = false;
                  pinned = false;
                });
              },
              child: Text("不固定"),
            ),
            SizedBox(
              height: 15,
            ),
            FloatingActionButton(
              onPressed: () {
                floating = true;
                pinned = false;
              },
              child: Text("浮动"),
            ),
            SizedBox(
              height: 15,
            ),
            FloatingActionButton(
              onPressed: () {
                floating = false;
                pinned = false;
              },
              child: Text("不浮动"),
            ),
            SizedBox(
              height: 25,
            ),
          ],
        ),
      ),
    );
  }
}

class SliverAppBarDemoWidget extends StatefulWidget {
  final bool pinned;
  final bool floating;

  const SliverAppBarDemoWidget(this.pinned, this.floating, {Key? key}) : super(key: key);

  @override
  _SliverAppBarDemoWidgetState createState() => _SliverAppBarDemoWidgetState();
}

class _SliverAppBarDemoWidgetState extends State<SliverAppBarDemoWidget> {
  @override
  Widget build(BuildContext context) {
    return buildPostsListWidget();
  }

  PostsListWidget buildPostsListWidget() {
    return PostsListWidget((BuildContext context, AsyncSnapshot<List<Posts>> snapshot) {
      log("PostsListWidget buildPageContent run...");
      return buildCustomScrollView(snapshot);
    });
  }

  CustomScrollView buildCustomScrollView(AsyncSnapshot<List<Posts>> asyncsnapshot) {
    return CustomScrollView(
      slivers: [
        SliverAppBar(
          automaticallyImplyLeading: false,
          title: Text("NingHao"),
          centerTitle: true,
          pinned: widget.pinned,
          // 固定位置显示
          floating: widget.floating, // 浮动显示
        ),
        // 去除顶端的空白
        MediaQuery.removePadding(
          context: context,
          child: SliverSafeArea(
              // 显示在安全区域
              // 添加内边距
              sliver: SliverListDemo(asyncsnapshot.data!)),
          removeTop: true,
        )
      ],
    );
  }
}
